package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import util.enumeration.Language;
import util.enumeration.OperatingDay;
import util.enumhelper.SupportedLocale;
import util.localization.WebLocalization;


/**
 *
 * @author Amelia
 */
@FacesConverter(value = "operatingDayConverter", forClass = util.enumeration.OperatingDay.class)
public class OperatingDayConverter implements Converter {
    
    private WebLocalization localization;
    private String preferredLocale;
    
    public OperatingDayConverter() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
    }

    
    //getAsObject method converts a string to a an object
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {    

        if (value == null || value.length() == 0) {
            return null;
        }
  
        // en_US ENGLISH, UNITED STATES
        if (preferredLocale.equals(SupportedLocale.getSupportedLocale(Language.ENGLISH_US))) {
            return getOperatingDayAsObject_EN_US(value);
        }
        // en_SG ENGLISH, SINGAPORE
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocale(Language.ENGLISH_SINGAPORE))) {
            return getOperatingDayAsObject_EN_SG(value);
        }
        // ms_MY MALAY, MALAYSIA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocale(Language.MALAY))) {
            return getOperatingDayAsObject_MS_MY(value);
        }

        // zh_CN CHINESE, CHINA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocale(Language.CHINESE_CHINA))) {
            return getOperatingDayAsObject_ZH_CN(value);
        }
        // other language...
        else {
            return getOperatingDayAsObject_EN_US(value);
        }
    }
    
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value instanceof OperatingDay) {
            OperatingDay operatingDay = (OperatingDay) value;

        String localizedOperatingDay = "";
        
        // en_US ENGLISH, UNITED STATES
        if (preferredLocale.equals(SupportedLocale.getSupportedLocale(Language.ENGLISH_US))) {
            localizedOperatingDay = getOperatingDayAsString_EN_US(operatingDay);
        }
        // en_SG ENGLISH, SINGAPORE
        else if (preferredLocale.equals(SupportedLocale.getSupportedLocale(Language.ENGLISH_SINGAPORE))) {
            localizedOperatingDay = getOperatingDayAsString_EN_SG(operatingDay);
        }
        // ms_MY MALAY, MALAYSIA
        else if (preferredLocale.equals(SupportedLocale.getSupportedLocale(Language.MALAY))) {
            localizedOperatingDay = getOperatingDayAsString_MS_MY(operatingDay);
        }
        
        // zh_CN CHINESE, CHINA
        else if (preferredLocale.equals(SupportedLocale.getSupportedLocale(Language.CHINESE_CHINA))) {
            localizedOperatingDay = getOperatingDayAsString_ZH_CN(operatingDay);
        }
        // other language...
        else {
            localizedOperatingDay = getOperatingDayAsString_EN_US(operatingDay);
        }
         return localizedOperatingDay;
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.OperatingDay");
        }
    }
    
    // simplified chinese (zh_CN)
    private static Object getOperatingDayAsObject_ZH_CN(String value) {
        if(value.equals("星期一")) return OperatingDay.MONDAY;
        else if(value.equals("星期二")) return OperatingDay.TUESDAY;
        else if(value.equals("星期三")) return OperatingDay.WEDNESDAY;
        else if(value.equals("星期四")) return OperatingDay.THURSDAY;
        else if(value.equals("星期五")) return OperatingDay.FRIDAY;
        else if(value.equals("星期六")) return OperatingDay.SATURDAY;
        else return OperatingDay.SUNDAY;
    }
    
    // default (en_US)
    private static Object getOperatingDayAsObject_EN_US(String value) {
        if(value.equals("MONDAY")) return OperatingDay.MONDAY;
        else if(value.equals("TUESDAY")) return OperatingDay.TUESDAY;
        else if(value.equals("WEDNESDAY")) return OperatingDay.WEDNESDAY;
        else if(value.equals("THURSDAY")) return OperatingDay.THURSDAY;
        else if(value.equals("FRIDAY")) return OperatingDay.FRIDAY;
        else if(value.equals("SATURDAY")) return OperatingDay.SATURDAY;
        else return OperatingDay.SUNDAY;
    }
    
        
    // singapore english (en_SG)
    private static Object getOperatingDayAsObject_EN_SG(String value) {
        if(value.equals("MONDAY")) return OperatingDay.MONDAY;
        else if(value.equals("TUESDAY")) return OperatingDay.TUESDAY;
        else if(value.equals("WEDNESDAY")) return OperatingDay.WEDNESDAY;
        else if(value.equals("THURSDAY")) return OperatingDay.THURSDAY;
        else if(value.equals("FRIDAY")) return OperatingDay.FRIDAY;
        else if(value.equals("SATURDAY")) return OperatingDay.SATURDAY;
        else return OperatingDay.SUNDAY;
    }
    
    // bahasa malaysia (ms_MY)
    private static Object getOperatingDayAsObject_MS_MY(String value) {
        if(value.equals("Isnin")) return OperatingDay.MONDAY;
        else if(value.equals("Selasa")) return OperatingDay.TUESDAY;
        else if(value.equals("Rabu")) return OperatingDay.WEDNESDAY;
        else if(value.equals("Khamis")) return OperatingDay.THURSDAY;
        else if(value.equals("Jumaat")) return OperatingDay.FRIDAY;
        else if(value.equals("Sabtu")) return OperatingDay.SATURDAY;
        else return OperatingDay.SUNDAY;
    }
    
    
     // simplified chinese (zh_CN)
    private static String getOperatingDayAsString_ZH_CN(OperatingDay operatingDay) {
        String localizedOperatingDay = "";
        
        switch (operatingDay) {
            case MONDAY: localizedOperatingDay = "星期一" ; break;
            case TUESDAY: localizedOperatingDay = "星期二"; break;
            case WEDNESDAY: localizedOperatingDay = "星期三"; break;
            case THURSDAY: localizedOperatingDay = "星期四"; break;
            case FRIDAY: localizedOperatingDay = "星期五"; break;
            case SATURDAY: localizedOperatingDay = "星期六"; break;
            case SUNDAY: localizedOperatingDay = "星期日"; break;
         }
        return localizedOperatingDay;
    }
    
    // default (en_US)
    private static String getOperatingDayAsString_EN_US(OperatingDay operatingDay) {
        String localizedOperatingDay = "";
        
        switch (operatingDay) {
            case MONDAY: localizedOperatingDay = "MONDAY" ; break;
            case TUESDAY: localizedOperatingDay = "TUESDAY"; break;
            case WEDNESDAY: localizedOperatingDay = "WEDNESDAY"; break;
            case THURSDAY: localizedOperatingDay = "THURSDAY"; break;
            case FRIDAY: localizedOperatingDay = "FRIDAY"; break;
            case SATURDAY: localizedOperatingDay = "SATURDAY"; break;
            case SUNDAY: localizedOperatingDay = "SUNDAY"; break;
         }
        return localizedOperatingDay;
    }
    
    // singapore english (en_SG)
    private static String getOperatingDayAsString_EN_SG(OperatingDay operatingDay) {
        String localizedOperatingDay = "";
        
        switch (operatingDay) {
            case MONDAY: localizedOperatingDay = "MONDAY" ; break;
            case TUESDAY: localizedOperatingDay = "TUESDAY"; break;
            case WEDNESDAY: localizedOperatingDay = "WEDNESDAY"; break;
            case THURSDAY: localizedOperatingDay = "THURSDAY"; break;
            case FRIDAY: localizedOperatingDay = "FRIDAY"; break;
            case SATURDAY: localizedOperatingDay = "SATURDAY"; break;
            case SUNDAY: localizedOperatingDay = "SUNDAY"; break;
         }
        return localizedOperatingDay;
    }
    
    // bahasa malaysia (ms_MY)
    private static String getOperatingDayAsString_MS_MY(OperatingDay operatingDay) {
        String localizedOperatingDay = "";
        
        switch (operatingDay) {
            case MONDAY: localizedOperatingDay = "Isnin" ; break;
            case TUESDAY: localizedOperatingDay = "Selasa"; break;
            case WEDNESDAY: localizedOperatingDay = "Rabu"; break;
            case THURSDAY: localizedOperatingDay = "Khamis"; break;
            case FRIDAY: localizedOperatingDay = "Jumaat"; break;
            case SATURDAY: localizedOperatingDay = "Sabtu"; break;
            case SUNDAY: localizedOperatingDay = "Ahad"; break;
         }
        return localizedOperatingDay;
    }
}